package com.jbit.hxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jbit.hxy.common.Result;
import com.jbit.hxy.model.dto.AdminFromDTO;
import com.jbit.hxy.model.dto.AdminLoginDTO;
import com.jbit.hxy.model.dto.AdminQueryDTO;
import com.jbit.hxy.model.po.Admin;
import com.jbit.hxy.service.user.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Tag(name = "管理员模块")
public class AdminController {
    private final AdminService adminService;
    @PostMapping("/list")
    @Operation(summary = "管理员列表",description = "分页查询管理员列表")
    public Result getAdminList(@RequestBody AdminQueryDTO adminQueryDTO) {
        return adminService.getAdminList(adminQueryDTO);

    }
    @PostMapping("/add")
    @Operation(summary = "添加管理员",description = "添加管理员")
    public Result addAdmin(@RequestBody AdminFromDTO adminFromDTO) {
        return adminService.addAdmin(adminFromDTO);

    }
    @PostMapping("/update")
    @Operation(summary = "修改管理员",description = "修改管理员")
    public Result updateAdmin(@RequestBody AdminFromDTO adminFromDTO) {
        return adminService.updateAdmin(adminFromDTO);
    }
    @GetMapping("/delete")
    @Operation(summary = "删除管理员",description = "删除管理员")
    public Result deleteAdmin(@RequestParam("id") Long id) {
        return adminService.deleteAdmin(id);
    }
    @PostMapping("/login")
    @Operation(summary = "管理员登录",description = "管理员登录")
    public Result login(@RequestBody AdminLoginDTO adminLoginDTO) {
        LambdaQueryWrapper<Admin> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Admin::getUserName, adminLoginDTO.getUserName());
        Admin admins = adminService.getOne(lqw);

        if (Objects.isNull(admins)) {
            return Result.fail("用户不存在，请重新输入");
        } else {
            if (!adminLoginDTO.getPassword().equals(admins.getPassword())) {
                return Result.fail("密码错误,请重新输入");
            }else {
                // 第3步，返回给前端
                return Result.success("登录成功");
            }
        }
    }
 }
